package com.server.core;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import org.springframework.web.server.WebFilter;
import org.springframework.web.server.WebFilterChain;
import reactor.core.publisher.Mono;

import javax.validation.constraints.NotNull;

/**
 * @author WuFengSheng
 * @date 2025/4/22 09:52
 */
@Slf4j
@Component
public class SseWebFilter implements WebFilter {

    @Override
    public Mono<Void> filter(@NotNull ServerWebExchange exchange, WebFilterChain chain) {
        String acceptHeader = exchange.getRequest().getHeaders().getFirst("Accept");
        boolean isSse = acceptHeader.contains("text/event-stream");
        if (isSse) {
            log.info("URI={}", exchange.getRequest().getURI());
            log.info("path={}", exchange.getRequest().getPath());
            log.info("method={}", exchange.getRequest().getMethod());
            log.info("headers={}", exchange.getRequest().getHeaders());
            log.info("queryParams={}", exchange.getRequest().getQueryParams());
        }
        return chain.filter(exchange);
    }

}
